﻿//In Module Pattern, we create one Global Object in that we augment our public and private methods. We create a singleton instance of that object by writing self-executed method. 
//See below code how we can write self-executed method
//1: 声明,通过建立一个匿名自执行的函数来声明这个module, 声明后立即执行
//2: 暴露,私有变量是隐藏的, 对外只暴露出一个公共方法
var Module = (function () {
    var privateVar = 'this is Private';
    var privateMethod = function () {
        //doSomething
    };

    return {
        publicMethod: function () {
            alert(privateVar);
            privateMethod();
        }
    }

})();         //self-executed method

//一个扩展, 当需要多个文档同时实现这个模型时
var module_ext = (function (my) {
    // add capabilities...          
    return my;
} (module_ext || {}));


//当需要对已有设计进行重洗时, 同时也保持了旧有的方法的指针
var MODULE = (function (my) {
    var old_moduleMethod = my.moduleMethod;
    my.moduleMethod = function () {
        // method override, has access to old through old_moduleMethod...     
    };
    return my;
} (MODULE));


// reference:
// JavaScript Module Pattern : http://www.codeproject.com/KB/scripting/ModulePattern.aspx
// JavaScript Module Pattern: In-Depth  http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

